<template>
  <div>
    <el-dialog
      width="1024px"
      :close-on-click-modal="false"
      :show-close="true"
      v-bind="$attrs"
      v-on="$listeners"
      @open="onOpen"
      @close="onClose"
    >
      <div slot="title">
        {{ formData.id != null ? '修改产品' : '新增产品' }}
      </div>
      <div class="sketch_content">
        <el-form label-position="top" ref="elForm" :model="formData" :rules="rules" label-width="80px">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="产品名称" prop="productName">
                <el-input v-model="formData.productName" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="产品编码" prop="productSn">
                <el-input v-model="formData.productSn" prefix-icon="el-icon-mobile-phone" style="width: 100%"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="产品单位" prop="unit">
                <el-select v-model="formData.unit" placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.sys_crm_product_unit"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="产品类型" prop="categoryId">
                <el-select v-model="formData.categoryId" placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.sys_crm_product_category"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="价格" prop="price">
                <el-input v-model="formData.price" prefix-icon="el-icon-message" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否上下架" prop="status">
                <el-select v-model="formData.status" placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.sys_crm_grounding"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="产品描述" prop="description">
                <el-input
                  type="textarea"
                  :autosize="{ minRows: 3, maxRows: 5}"
                  placeholder="请输入内容"
                  v-model="formData.description"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="产品详情图片" prop="description">
                <image-upload :limit="8"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div style="clear: both"></div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="submitForm" :loading="submitLoading">保 存</el-button>
        <el-button size="small" @click="onClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  saveHasKey,
  selectByPrimaryKey,
  updateByPrimaryKeySelective
} from '@/api/crm/Product'

import ImageUpload from '@/components/ImageUpload/index'

export default {
  inheritAttrs: false,
  name: 'productDialog',
  components: {
    ImageUpload
  },
  dicts: ['sys_crm_grounding', 'sys_crm_product_unit', 'sys_crm_product_category'],
  props: {
    pk: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      title: '添加产品',
      dialogVisible: false,
      submitLoading: false,
      formData: {
        id: undefined,// 主键id
        productName: undefined,// 产品名称
        productSn: undefined,// 产品编码
        unit: undefined,// 产品单位
        categoryId: undefined,// 分类id
        categoryName: undefined,// 分类名称
        price: undefined,// 产品价格
        status: undefined,// 是否上下架
        description: undefined// 产品描述
      },
      rules: {
        productName: [{ required: true, message: '产品名称不能为空', trigger: 'blur' }],
        categoryId: [{ required: true, message: '产品类型不能为空', trigger: 'blur' }],
        price: [{ required: true, message: '产品价格不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '是否上下架不能为空', trigger: 'blur' }]
      },
      // 线索来源数据
      sourceNameDict: [],
      // 客户行业数据
      customerIndustryDict: [],
      // 客户等级数据
      customerLevelDict: []
    }
  },
  // 在实例创建完成后被立即调用
  created() {
  },
  // 实例被挂在后调用
  mounted() {

  },
  // 方法
  methods: {
    submitForm() {
      this.submitLoading = true
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (this.formData.id != undefined) {
            // 修改
            updateByPrimaryKeySelective(this.formData).then(res => {
              this.$emit('ok')
              this.onClose()
              this.$modal.msgSuccess('修改成功')
            })
          } else {
            // 新增
            saveHasKey(this.formData).then(res => {
              this.$emit('ok')
              this.onClose()
              this.$modal.msgSuccess('添加成功')
            })
          }
        }
      })
    },
    // 对话框打开
    onOpen() {
      if (this.pk != null) {
        selectByPrimaryKey(this.pk).then(res => {
          this.formData = res.data
        })
      }
    },
    // 对话框关闭
    onClose() {
      this.$refs['elForm'].resetFields()
      this.$emit('update:visible', false)
      this.submitLoading = false
    }
  }
}
</script>

<style scoped>

.sketch_content {
  overflow: auto;
  height: 500px;
  border-top: 1px solid #eff1f4;
  border-bottom: 1px solid #eff1f4;
  padding: 0px 20px 11px 27px;
  margin: -30px -20px 0px -20px;
}

.sketch_content::-webkit-scrollbar {
  width: 8px;
}

.sketch_content::-webkit-scrollbar-thumb {
  background: #8798AF;
  border-radius: 10px;
}

.sketch_content::-webkit-scrollbar-track {
  background: transparent;
}
</style>
